package com.lpb.spring.test1.factory.support.classinit;

import com.lpb.spring.test1.factory.config.BeanDefinition;

import java.lang.reflect.Constructor;

/**
 * @author linpengbo
 * @date 2022/4/29
 */
public class JDKInstantiationStrategy implements InstantiationStrategy {


    @Override
    public Object initinstance(BeanDefinition definition, Constructor ct, Object[] args) {
        Class<?> clazz = definition.getClazz();
        try{
            if(ct==null){
                return clazz.getDeclaredConstructor().newInstance();
            }
            return clazz.getDeclaredConstructor(ct.getParameterTypes()).newInstance(args);
        }catch (Exception e){
           throw new RuntimeException("["+clazz.getName()+"]类初始化失败");
        }
    }
}
